package richbar.com.richbar.adapter.shopping;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.util.ToastUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.product.FirmOrderShopCartAc;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.NumBean;
import richbar.com.richbar.bean.ShopCartListBean;
import richbar.com.richbar.bean.TotalBean;
import richbar.com.richbar.fragment.ShoppingCartFragment;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by Administrator on 2018/12/11.
 */

public class ShoppingsAdapter extends BaseExpandableListAdapter {
    private Activity activity;
    private List<ShopCartListBean.DataBean.ListBean> groups;
    private List<List<ShopCartListBean.DataBean.ListBean.GoodsBean>> childs;
    private LoadingDialog dialog;
    ShoppingCartFragment fragment;
    TextView payMoneyLeft;

    public ShoppingsAdapter(final Activity activity, final List<ShopCartListBean.DataBean.ListBean> groups, final List<List<ShopCartListBean.DataBean.ListBean.GoodsBean>> child, TextView payMoneyLeft) {
        this.activity = activity;
        this.groups = groups;
        this.childs = child;
        this.payMoneyLeft = payMoneyLeft;
        dialog = new LoadingDialog(activity, 0);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final ShoppingsAdapter.GroupViewHolder holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_shopping_card_parents, parent, false);
            holder = new ShoppingsAdapter.GroupViewHolder();
            holder.name = convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        } else {
            holder = (ShoppingsAdapter.GroupViewHolder) convertView.getTag();
        }
        holder.name.setText(groups.get(groupPosition).getSellername());

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ShoppingsAdapter.ChildViewHolder holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_shopping_card_childs, parent, false);
            holder = new ShoppingsAdapter.ChildViewHolder();
            holder.name = convertView.findViewById(R.id.name);

            holder.pic = convertView.findViewById(R.id.pic);
            holder.price = convertView.findViewById(R.id.price);

            holder.tv_minute = convertView.findViewById(R.id.tv_minute);
            holder.tv_plus = convertView.findViewById(R.id.tv_plus);
            holder.edit_num = convertView.findViewById(R.id.edit_nums);
            holder.ly = convertView.findViewById(R.id.ly);
            convertView.setTag(holder);
        } else {
            holder = (ShoppingsAdapter.ChildViewHolder) convertView.getTag();
        }
        holder.edit_num.setText(childs.get(groupPosition).get(childPosition).getGoods_num() + "");
        holder.price.setText(childs.get(groupPosition).get(childPosition).getGoods_price());
        holder.name.setText(childs.get(groupPosition).get(childPosition).getGoods_name());
        GlideUtil.loadImage(activity, NetWorkUtil.URL + childs.get(groupPosition).get(childPosition).getGoods_img(), holder.pic);
        holder.ly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n = Integer.valueOf(holder.edit_num.getText().toString());
                if (n > 1) {
                    n = n - 1;
                    holder.edit_num.setText(n + "");
                    NetWorkUtil.UpdateCart(BaseApplication.uid, childs.get(groupPosition).get(childPosition).getCart_id() + "", n + "", Util.md5(), new HttpUtils.HttpCallback() {
                        @Override
                        public void onSuccess(String data) {
                            final NumBean numBean = MyGson.getInstance().fromJson(data, NumBean.class);
                            if (numBean.getCode() == 200) {
                                payMoneyLeft.setText("合计：" + numBean.getData().getTotalPrice());
                            }
                        }

                        @Override
                        public void onError(String msg) {
                            super.onError(msg);
                            ToastUtil.showToast(activity, msg);
                        }
                    });

                }

            }

        });
        holder.tv_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n = Integer.valueOf(holder.edit_num.getText().toString());
                n = n + 1;
                holder.edit_num.setText(n + "");
                NetWorkUtil.UpdateCart(BaseApplication.uid, childs.get(groupPosition).get(childPosition).getCart_id() + "", n + "", Util.md5(), new HttpUtils.HttpCallback() {
                    @Override
                    public void onSuccess(String data) {
                        final NumBean numBean = MyGson.getInstance().fromJson(data, NumBean.class);
                        if (numBean.getCode() == 200) {
                            payMoneyLeft.setText("合计：" + numBean.getData().getTotalPrice());
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        super.onError(msg);
                    }
                });
            }
        });

        return convertView;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        TextView name;
    }

    class ChildViewHolder {
        TextView name;
        TextView price;
        ImageView pic;
        TextView tv_minute;
        TextView tv_plus;
        EditText edit_num;
        LinearLayout ly;
    }
}
